package com.service.sys;

import com.common.ErrorCodeEnum;
import com.common.emums.DataStatus;
import com.common.exception.BusinessException;
import com.core.springSecurity.common.SecurityUtils;
import com.dao.primary.sys.SysRoleMenuMapper;
import com.model.pojo.sys.SysMenu;
import com.model.pojo.sys.SysRole;
import com.model.pojo.sys.SysRoleMenu;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;


/**
 * @Author: zql
 * @Description: 角色菜单关联服务
 * @Data: 2021-07-08
 */
@Slf4j
@Service
public class SysRoleMenuService {

    @Resource
    SysRoleMenuMapper sysRoleMenuMapper;

    @Resource
    SysMenuService sysMenuService;

    /**
     * @Author: zql
     * @Description: 校验队列中菜单是否存在
     * @param menuIdSet 菜单id列表
     * @Data: 10:06 2021/5/12
     */
    public void verifySysMenuId(Set<Long> menuIdSet) {
        menuIdSet.forEach(menuId -> {
            if (!sysMenuService.has(menuId, DataStatus.OK.getCode())) {
                if (sysMenuService.has(menuId, DataStatus.DISABLE.getCode())) {
                    SysMenu sysMenu = sysMenuService.get(menuId, null);
                    throw new BusinessException(ErrorCodeEnum.MENU_STOP.setParam(menuId.toString(), sysMenu.getMenuName()));
                }
                if (sysMenuService.has(menuId, DataStatus.DELETED.getCode())) {
                    SysMenu sysMenu = sysMenuService.get(menuId, null);
                    throw new BusinessException(ErrorCodeEnum.MENU_DELETE.setParam(menuId.toString(), sysMenu.getMenuName()));
                }
                throw new BusinessException(ErrorCodeEnum.MENU_NOT_FOUND);
            }
        });
    }

    /**
     * @Author: zql
     * @Description: 根据角色id获取菜单列表
     * @param sysRoleId 角色id
     * @Data: 14:46 2021/7/16
     */
    public List<SysMenu> getMenuList(Long sysRoleId){
        List<SysRoleMenu> sysRoleMenuList = sysRoleMenuMapper.selectListByRoleId(sysRoleId);
        List<SysMenu> sysMenuList = new ArrayList<>();
        for (SysRoleMenu sysRoleMenu : sysRoleMenuList) {
            try {
                SysMenu sysMenu = sysMenuService.get(sysRoleMenu.getSysMenuId(), null);
                if (DataStatus.OK.equals(sysMenu.getIsDelete())) {
                    sysMenuList.add(sysMenu);
                } else {
                    log.error("无法根据角色获取菜单，角色绑定的菜单状态:{} roleId:{},menuId:{}", DataStatus.info(sysMenu.getIsDelete()), sysRoleId, sysMenu.getSysMenuId());
                }
            } catch (BusinessException e) {
                log.error("无法根据角色获取菜单，角色绑定了不存在的菜单roleId:{},menuId:{},code:{},msg:{}", sysRoleId, sysRoleMenu.getSysMenuId(),
                        e.getErrorCodeEnum().getErrorCode(), e.getErrorCodeEnum().getErrorMsg());
            }
        }
        return sysMenuList;
    }

    /**
     * @Author: zql
     * @Description: 根据一组角色id获取菜单列表
     * @param sysRoleSet 角色id集合
     * @Data: 14:47 2021/7/16
     */
    public Set<SysMenu> getMenuSet(Set<SysRole> sysRoleSet){
        Set<SysMenu> sysMenuSet=new HashSet<>();
        for(SysRole sysRole:sysRoleSet){
            List<SysMenu> sysMenuList= getMenuList(sysRole.getSysRoleId());
            sysMenuSet.addAll(sysMenuList);
        }
        return sysMenuSet;
    }

    public void uBindList(Long roleId){
        sysRoleMenuMapper.deleteBySysRoleId(roleId);
    }

    /**
     * @Author: zql
     * @Description: 批量绑定角色与菜单
     * @param roleId 角色id
     * @param menuIdS 部门id列表
     * @Data: 14:46 2021/7/8
     */
    public void binding(Long roleId, Set<Long> menuIdS) {
        if (menuIdS.size() == 0) {
            return;
        }
        String name = SecurityUtils.getUsername();
        List<SysRoleMenu> sysRoleMenuList = new ArrayList<>();
        for (Long menuId : menuIdS) {
            SysRoleMenu sysRoleMenu = new SysRoleMenu();
            sysRoleMenu.setSysRoleId(roleId);
            sysRoleMenu.setSysMenuId(menuId);
            sysRoleMenu.setModifier(name);
            sysRoleMenu.setCreator(name);
            sysRoleMenuList.add(sysRoleMenu);
        }
        sysRoleMenuMapper.insertListSelective(sysRoleMenuList);
    }
}
